package parse

/*
import (
	"reflect"
	"showen/ast"
	"showen/lex"
	"showen/tok"
	"testing"
)

func TestParseExpression(t *testing.T) {
	tests := []struct {
		name          string
		tokens        []tok.Token
		expected      ast.Exp
		expectError   bool
		errorContains string
	}{
		{
			name: "identifier expression",
			tokens: []tok.Token{
				{Type: tok.IDENTIFIER, Lexeme: "x", Line: 1, Column: 1},
				{Type: tok.EOF, Line: 1, Column: 2},
			},
			expected: &ast.IdentifierExp{
				Token: tok.Token{Type: tok.IDENTIFIER, Lexeme: "x", Line: 1, Column: 1},
			},
			expectError: false,
		},
		{
			name: "boolean true expression",
			tokens: []tok.Token{
				{Type: tok.BOOLEAN, Lexeme: "#t", Line: 1, Column: 1},
				{Type: tok.EOF, Line: 1, Column: 2},
			},
			expected: &ast.BooleanExp{
				Token: tok.Token{Type: tok.BOOLEAN, Lexeme: "#t", Line: 1, Column: 1},
			},
			expectError: false,
		},
		{
			name: "boolean false expression",
			tokens: []tok.Token{
				{Type: tok.BOOLEAN, Lexeme: "#f", Line: 1, Column: 1},
				{Type: tok.EOF, Line: 1, Column: 2},
			},
			expected: &ast.BooleanExp{
				Token: tok.Token{Type: tok.BOOLEAN, Lexeme: "#f", Line: 1, Column: 1},
			},
			expectError: false,
		},
		{
			name: "empty list expression",
			tokens: []tok.Token{
				{Type: tok.LPAREN, Lexeme: "(", Line: 1, Column: 1},
				{Type: tok.RPAREN, Lexeme: ")", Line: 1, Column: 2},
				{Type: tok.EOF, Line: 1, Column: 3},
			},
			expected: &ast.ListExp{
				Exps: []ast.Exp{},
			},
			expectError: false,
		},
		{
			name: "list with one identifier",
			tokens: []tok.Token{
				{Type: tok.LPAREN, Lexeme: "(", Line: 1, Column: 1},
				{Type: tok.IDENTIFIER, Lexeme: "x", Line: 1, Column: 2},
				{Type: tok.RPAREN, Lexeme: ")", Line: 1, Column: 3},
				{Type: tok.EOF, Line: 1, Column: 4},
			},
			expected: &ast.ListExp{
				Exps: []ast.Exp{
					&ast.IdentifierExp{
						Token: tok.Token{Type: tok.IDENTIFIER, Lexeme: "x", Line: 1, Column: 2},
					},
				},
			},
			expectError: false,
		},
		{
			name: "list with multiple elements",
			tokens: []tok.Token{
				{Type: tok.LPAREN, Lexeme: "(", Line: 1, Column: 1},
				{Type: tok.IDENTIFIER, Lexeme: "define", Line: 1, Column: 2},
				{Type: tok.IDENTIFIER, Lexeme: "x", Line: 1, Column: 3},
				{Type: tok.IDENTIFIER, Lexeme: "1", Line: 1, Column: 4},
				{Type: tok.RPAREN, Lexeme: ")", Line: 1, Column: 5},
				{Type: tok.EOF, Line: 1, Column: 6},
			},
			expected: &ast.ListExp{
				Exps: []ast.Exp{
					&ast.IdentifierExp{
						Token: tok.Token{Type: tok.IDENTIFIER, Lexeme: "define", Line: 1, Column: 2},
					},
					&ast.IdentifierExp{
						Token: tok.Token{Type: tok.IDENTIFIER, Lexeme: "x", Line: 1, Column: 3},
					},
					&ast.IdentifierExp{
						Token: tok.Token{Type: tok.IDENTIFIER, Lexeme: "1", Line: 1, Column: 4},
					},
				},
			},
			expectError: false,
		},
		{
			name: "nested list expression",
			tokens: []tok.Token{
				{Type: tok.LPAREN, Lexeme: "(", Line: 1, Column: 1},
				{Type: tok.LPAREN, Lexeme: "(", Line: 1, Column: 2},
				{Type: tok.IDENTIFIER, Lexeme: "x", Line: 1, Column: 3},
				{Type: tok.RPAREN, Lexeme: ")", Line: 1, Column: 4},
				{Type: tok.RPAREN, Lexeme: ")", Line: 1, Column: 5},
				{Type: tok.EOF, Line: 1, Column: 6},
			},
			expected: &ast.ListExp{
				Exps: []ast.Exp{
					&ast.ListExp{
						Exps: []ast.Exp{
							&ast.IdentifierExp{
								Token: tok.Token{Type: tok.IDENTIFIER, Lexeme: "x", Line: 1, Column: 3},
							},
						},
					},
				},
			},
			expectError: false,
		},
		{
			name: "integer expression",
			tokens: []tok.Token{
				{Type: tok.INTEGER, Lexeme: "123", Literal: int64(123), Line: 1, Column: 1},
				{Type: tok.EOF, Line: 1, Column: 2},
			},
			expected: &ast.NumberExp{
				Token: tok.Token{Type: tok.INTEGER, Lexeme: "123", Literal: int64(123), Line: 1, Column: 1},
			},
			expectError: false,
		},
		{
			name: "negative integer expression",
			tokens: []tok.Token{
				{Type: tok.INTEGER, Lexeme: "-456", Literal: int64(-456), Line: 1, Column: 1},
				{Type: tok.EOF, Line: 1, Column: 2},
			},
			expected: &ast.NumberExp{
				Token: tok.Token{Type: tok.INTEGER, Lexeme: "-456", Literal: int64(-456), Line: 1, Column: 1},
			},
			expectError: false,
		},
		{
			name: "zero integer expression",
			tokens: []tok.Token{
				{Type: tok.INTEGER, Lexeme: "0", Literal: int64(0), Line: 1, Column: 1},
				{Type: tok.EOF, Line: 1, Column: 2},
			},
			expected: &ast.NumberExp{
				Token: tok.Token{Type: tok.INTEGER, Lexeme: "0", Literal: int64(0), Line: 1, Column: 1},
			},
			expectError: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			parser := NewParser(tt.tokens)
			exp, err := parser.ParseExpression()

			if tt.expectError {
				if err == nil {
					t.Fatalf("expected error but got none")
				}
				if tt.errorContains != "" && err.Error() != tt.errorContains {
					t.Errorf("expected error containing %q, got %q", tt.errorContains, err.Error())
				}
				return
			}

			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}

			if !reflect.DeepEqual(exp, tt.expected) {
				t.Errorf("ParseExpression() = %v, want %v", exp, tt.expected)
				t.Errorf("ParseExpression() type = %T, want %T", exp, tt.expected)
			}
		})
	}
}

func TestParseProgram(t *testing.T) {
	tests := []struct {
		name        string
		tokens      []tok.Token
		expected    []ast.Exp
		expectError bool
	}{
		{
			name: "empty program",
			tokens: []tok.Token{
				{Type: tok.EOF, Line: 1, Column: 1},
			},
			expected:    []ast.Exp{},
			expectError: false,
		},
		{
			name: "program with one expression",
			tokens: []tok.Token{
				{Type: tok.IDENTIFIER, Lexeme: "x", Line: 1, Column: 1},
				{Type: tok.EOF, Line: 1, Column: 2},
			},
			expected: []ast.Exp{
				&ast.IdentifierExp{
					Token: tok.Token{Type: tok.IDENTIFIER, Lexeme: "x", Line: 1, Column: 1},
				},
			},
			expectError: false,
		},
		{
			name: "program with multiple expressions",
			tokens: []tok.Token{
				{Type: tok.IDENTIFIER, Lexeme: "x", Literal: "x", Line: 1, Column: 1},
				{Type: tok.IDENTIFIER, Lexeme: "y", Literal: "y", Line: 1, Column: 3},
				{Type: tok.EOF, Line: 1, Column: 4},
			},
			expected: []ast.Exp{
				&ast.IdentifierExp{
					Token: tok.Token{Type: tok.IDENTIFIER, Lexeme: "x", Literal: "x", Line: 1, Column: 1},
				},
				&ast.IdentifierExp{
					Token: tok.Token{Type: tok.IDENTIFIER, Lexeme: "y", Literal: "y", Line: 1, Column: 3},
				},
			},
			expectError: false,
		},
		{
			name: "program with list expression",
			tokens: []tok.Token{
				{Type: tok.LPAREN, Lexeme: "(", Line: 1, Column: 1},
				{Type: tok.IDENTIFIER, Lexeme: "define", Line: 1, Column: 2},
				{Type: tok.IDENTIFIER, Lexeme: "x", Line: 1, Column: 3},
				{Type: tok.IDENTIFIER, Lexeme: "1", Line: 1, Column: 4},
				{Type: tok.RPAREN, Lexeme: ")", Line: 1, Column: 5},
				{Type: tok.EOF, Line: 1, Column: 6},
			},
			expected: []ast.Exp{
				&ast.ListExp{
					Exps: []ast.Exp{
						&ast.IdentifierExp{
							Token: tok.Token{Type: tok.IDENTIFIER, Lexeme: "define", Line: 1, Column: 2},
						},
						&ast.IdentifierExp{
							Token: tok.Token{Type: tok.IDENTIFIER, Lexeme: "x", Line: 1, Column: 3},
						},
						&ast.IdentifierExp{
							Token: tok.Token{Type: tok.IDENTIFIER, Lexeme: "1", Line: 1, Column: 4},
						},
					},
				},
			},
			expectError: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			parser := NewParser(tt.tokens)
			exprs, err := parser.ParseProgram()

			if tt.expectError {
				if err == nil {
					t.Fatalf("expected error but got none")
				}
				return
			}

			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}

			if !reflect.DeepEqual(exprs, tt.expected) {
				t.Errorf("ParseProgram() = %v, want %v", exprs, tt.expected)
			}
		})
	}
}

func TestParseList(t *testing.T) {
	tests := []struct {
		name          string
		tokens        []tok.Token
		expected      *ast.ListExp
		expectError   bool
		errorContains string
	}{
		{
			name: "empty list",
			tokens: []tok.Token{
				{Type: tok.LPAREN, Lexeme: "(", Line: 1, Column: 1},
				{Type: tok.RPAREN, Lexeme: ")", Line: 1, Column: 2},
				{Type: tok.EOF, Line: 1, Column: 3},
			},
			expected: &ast.ListExp{
				Exps: []ast.Exp{},
			},
			expectError: false,
		},
		{
			name: "list with elements",
			tokens: []tok.Token{
				{Type: tok.LPAREN, Lexeme: "(", Line: 1, Column: 1},
				{Type: tok.IDENTIFIER, Lexeme: "x", Line: 1, Column: 2},
				{Type: tok.RPAREN, Lexeme: ")", Line: 1, Column: 3},
				{Type: tok.EOF, Line: 1, Column: 4},
			},
			expected: &ast.ListExp{
				Exps: []ast.Exp{
					&ast.IdentifierExp{
						Token: tok.Token{Type: tok.IDENTIFIER, Lexeme: "x", Line: 1, Column: 2},
					},
				},
			},
			expectError: false,
		},
		{
			name: "missing closing parenthesis error",
			tokens: []tok.Token{
				{Type: tok.LPAREN, Lexeme: "(", Line: 1, Column: 1},
				{Type: tok.IDENTIFIER, Lexeme: "x", Line: 1, Column: 2},
				{Type: tok.EOF, Line: 1, Column: 3},
			},
			expected:      nil,
			expectError:   true,
			errorContains: "unexpected end of line 1, expected ')'",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			parser := NewParser(tt.tokens)
			// We need to manually advance to the LPAREN token to test parseList directly
			exp, err := parser.parseList()

			if tt.expectError {
				if err == nil {
					t.Fatalf("expected error but got none")
				}
				if tt.errorContains != "" && err.Error() != tt.errorContains {
					t.Errorf("expected error containing %q, got %q", tt.errorContains, err.Error())
				}
				return
			}

			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}

			if !reflect.DeepEqual(exp, tt.expected) {
				t.Errorf("parseList() = %v, want %v", exp, tt.expected)
			}
		})
	}
}

func TestParse001(t *testing.T) {
	tests := []struct {
		input string
		want  string
	}{
		{
			input: "#t",
			want:  "#t",
		},

		{
			input: "   	#t		",
			want:  "#t",
		},
		{
			input: "   	#f		",
			want:  "#f",
		},
		{
			input: "   	define		",
			want:  "define",
		},
		{
			input: "(not #t)",
			want:  "(not #t)",
		},
		{
			input: "(	not	 #t	)",
			want:  "(not #t)",
		},
		{
			input: "(define x (not #t))",
			want:  "(define x (not #t))",
		},
		{
			input: "(let x (not #t))",
			want:  "(let x (not #t))",
		},
		{
			input: "(let 1st-x (not #t))",
			want:  "(let 1st-x (not #t))",
		},
		{
			input: "(let 1st-x (not #t))",
			want:  "(let 1st-x (not #t))",
		},
		{
			input: "(define (f x) (not x))",
			want:  "(define (f x) (not x))",
		},
		{
			input: "(let ((x #f) (y #t)) (and x y))",
			want:  "(let ((x #f) (y #t)) (and x y))",
		}, //
	}

	for _, tt := range tests {
		lexer := lex.NewLexer(tt.input)
		tokens := lexer.ReadTokens()
		parser := NewParser(tokens)
		got, err := parser.ParseExpression()
		if err != nil {
			t.Fatalf("Parse() error = %v", err)
		}
		if got.String() != tt.want {
			t.Errorf("Parse() = %v, want %v", got, tt.want)
		}
	}
}
*/
